﻿// ***********************************************************************
// Assembly         : ResourceOptimizeLibrary
// Author           : ray
// Created          : 12-31-2014
//
// Last Modified By : ray
// Last Modified On : 12-31-2014
// ***********************************************************************
// <copyright file="CssOptimize.cs" company="radray">
//     Copyright (c) radray. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Yahoo.Yui.Compressor;

/// <summary>
/// The ResourceOptimizeLibrary namespace.
/// </summary>

namespace ResourceOptimizeLibrary
{
    /// <summary>
    ///     Class CssOptimize.
    /// </summary>
    public class CssOptimize : IOptimize
    {
        /// <summary>
        ///     Optimizes the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>System.String.</returns>
        public string Optimize(string path, Encoding encoding)
        {
            string result = string.Empty;
            if (Directory.Exists(path))
            {
                var filesArray = Directory.GetFiles(path);
                var sb = new StringBuilder();
                for (var i = 0; i < filesArray.Length; i++)
                {
                    if (File.Exists(filesArray[i]))
                    {
                        var fileContent = File.ReadAllText(filesArray[i], encoding);
                        if (!string.IsNullOrEmpty(fileContent))
                        {
                            try
                            {
                                fileContent = CssCompressor.Compress(fileContent, 73, CssCompressionType.Hybrid);
                            }
                            catch (Exception ex)
                            {
                            }
                            sb.Append(fileContent);
                        }
                    }
                }
                result = sb.ToString();
                if (!string.IsNullOrEmpty(result))
                {
                    try
                    {
                        result = CssCompressor.Compress(result, 73, CssCompressionType.Hybrid);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            return result;
        }

        /// <summary>
        ///     Optimizes the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>System.String.</returns>
        public string Optimize(string path)
        {
            return Optimize(path, Encoding.UTF8);
        }

        /// <summary>
        ///     Optimizes the specified file list.
        /// </summary>
        /// <param name="fileList">The file list.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>System.String.</returns>
        public string Optimize(List<string> fileList, Encoding encoding)
        {
            string result = string.Empty;
            var sb = new StringBuilder();
            for (var i = 0; i < fileList.Count; i++)
            {
                var path = fileList[i];
                if (File.Exists(path))
                {
                    var fileContent = File.ReadAllText(path, encoding);
                    if (!string.IsNullOrEmpty(fileContent))
                    {
                        try
                        {
                            fileContent = CssCompressor.Compress(fileContent, 73, CssCompressionType.Hybrid);
                        }
                        catch (Exception ex)
                        {
                        }
                        sb.Append(fileContent);
                    }
                }
            }
            result = sb.ToString();
            if (!string.IsNullOrEmpty(result))
            {
                try
                {
                    result = CssCompressor.Compress(result, 73, CssCompressionType.Hybrid);
                }
                catch (Exception ex)
                {
                }
            }
            return result;
        }

        /// <summary>
        ///     Optimizes the specified file list.
        /// </summary>
        /// <param name="fileList">The file list.</param>
        /// <returns>System.String.</returns>
        public string Optimize(List<string> fileList)
        {
            return Optimize(fileList, Encoding.UTF8);
        }

        /// <summary>
        ///     Optimizes the specified file list.
        /// </summary>
        /// <param name="fileList">The file list.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>System.String.</returns>
        public string Optimize(string[] fileList, Encoding encoding)
        {
            string result = string.Empty;
            var sb = new StringBuilder();
            for (var i = 0; i < fileList.Length; i++)
            {
                if (File.Exists(fileList[i]))
                {
                    var fileContent = File.ReadAllText(fileList[i], encoding);
                    if (!string.IsNullOrEmpty(fileContent))
                    {
                        try
                        {
                            fileContent = CssCompressor.Compress(fileContent, 73, CssCompressionType.Hybrid);
                        }
                        catch (Exception ex)
                        {
                        }
                        sb.Append(fileContent);
                    }
                }
            }
            result = sb.ToString();
            if (!string.IsNullOrEmpty(result))
            {
                try
                {
                    result = CssCompressor.Compress(result, 73, CssCompressionType.Hybrid);
                }
                catch (Exception ex)
                {
                }
            }
            return result;
        }

        /// <summary>
        ///     Optimizes the specified file list.
        /// </summary>
        /// <param name="fileList">The file list.</param>
        /// <returns>System.String.</returns>
        public string Optimize(string[] fileList)
        {
            return Optimize(fileList, Encoding.UTF8);
        }

        /// <summary>
        ///     Optimizes the by path list.
        /// </summary>
        /// <param name="pathList">The path list.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>System.String.</returns>
        public string OptimizeByPathList(List<string> pathList, Encoding encoding)
        {
            StringBuilder sb = new StringBuilder();
            string result = string.Empty;

            if (pathList != null && pathList.Count > 0)
            {
                for (var i = 0; i < pathList.Count; i++)
                {
                    sb.Append(Optimize(pathList[i], encoding));
                }
            }
            result = sb.ToString();
            if (!string.IsNullOrEmpty(result))
            {
                try
                {
                    result = CssCompressor.Compress(result, 73, CssCompressionType.Hybrid);
                }
                catch (Exception ex)
                {
                }
            }
            return result;
        }

        /// <summary>
        ///     Optimizes the by path list.
        /// </summary>
        /// <param name="pathList">The path list.</param>
        /// <returns>System.String.</returns>
        public string OptimizeByPathList(List<string> pathList)
        {
            return OptimizeByPathList(pathList, Encoding.UTF8);
        }

        /// <summary>
        ///     Optimizes the by path array.
        /// </summary>
        /// <param name="pathArray">The path array.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>System.String.</returns>
        public string OptimizeByPathArray(string[] pathArray, Encoding encoding)
        {
            string result = string.Empty;
            if (pathArray != null && pathArray.Length > 0)
            {
                var pathList = new List<string>(pathArray);
                result = OptimizeByPathList(pathList, encoding);
            }
            return result;
        }

        /// <summary>
        ///     Optimizes the by path array.
        /// </summary>
        /// <param name="pathArray">The path array.</param>
        /// <returns>System.String.</returns>
        public string OptimizeByPathArray(string[] pathArray)
        {
            return OptimizeByPathArray(pathArray, Encoding.UTF8);
        }

        /// <summary>
        ///     Writes the out.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="content">The content.</param>
        /// <param name="encoding">The encoding.</param>
        public void WriteOut(string path, string content, Encoding encoding)
        {
            if (File.Exists(path))
            {
                //  backup file first
                var tempFile = new FileInfo(path);
                tempFile.MoveTo(path + ".bak");
            }
            if (false == File.Exists(path))
            {
                File.WriteAllText(path, content, encoding);
            }
        }

        /// <summary>
        ///     Writes the out.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="content">The content.</param>
        public void WriteOut(string path, string content)
        {
            WriteOut(path, content, Encoding.UTF8);
        }
    }
}